package com.example.helloworld;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.helloworld.tools.Tools;

public class MainActivity extends Activity {

	private Context context;

	private ItemDataSource itemDataSource;

	// ui
	//
	private TextView fieldTitle;
	private TextView fieldPrice;
	private TextView fieldStatus;
	private TextView headerText;
	private Button updateObjectButtonOk;
	private Button updateObjectButtonCancel;
	private ListView myListView;
	private MyDataAdapter adapter;

	private Dialog dialogUpdate;
	private Dialog dialogCreate;
	private Dialog dialogSortSections;
	private Dialog dialogSortRelatedItemObjects;
	private Dialog dialogCreateNewSection;

	private TextView createNewObjectFieldTitle;
	private TextView createNewObjectFieldStatus;
	private TextView createNewObjectFieldPrice;
	private TextView createNewItemObjectFieldName;
	private ArrayList<ItemSection> createNewObjectSectionsList;

	private RadioButton sortSectionsDialogRadioButtonDesc;
	private RadioButton sortSectionsDialogRadioButtonAsc;
	private RadioButton sortRelatedItemsRadioButtonTitle;
	private RadioButton sortRelatedItemsRadioButtonStatus;
	private RadioButton sortRelatedItemsRadioButtonPrice;

	private ArrayList<ItemSection> sectionsList;
	private ArrayList<ItemObject> itemObjectsList;

	private int currentListPosition = 0;
	private int createNewObjectSpinnerPosition = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.incl);

		mapUI();

		itemDataSource = new ItemDataSource(context);

		prepareListeners();

		prepareList();

		registerForContextMenu(myListView);

		Tools.print("onCreate");
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Tools.print("onResume");
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Tools.print("onPause");
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Tools.print("onDestroy");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inf = new MenuInflater(context);
		inf.inflate(R.menu.menu_main_activity, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub

		switch (item.getItemId()) {
		case (R.id.menu_item_create_new_item):
			showCreateDialog();
			break;
		case (R.id.menu_item_create_new_section):
			showCreateNewSectionDialog();
			break;
		case (R.id.menu_item_sort_items):
			showSortSectionsDialog();
			break;

		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
		currentListPosition = info.position;
        
		MenuInflater menuInflater = getMenuInflater();
		Item currItem = adapter.getItem(currentListPosition);
		if (currItem instanceof ItemSection) {
			menuInflater.inflate(R.menu.contextmenu_listview_section, menu);
		} else if (currItem instanceof ItemObject) {
			menuInflater.inflate(R.menu.contextmenu_listview_object, menu);
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		int itemId = item.getItemId();
		Item currItem = adapter.getItem(currentListPosition);
		if (currItem instanceof ItemSection) {
			// TODO
			switch (itemId) {
			case R.id.contextmenu_listview_object_section_sort_related_itemobject:
				showSortRelatedItemObjectsDialog(currentListPosition);
				break;
			case R.id.contextmenu_listview_object_section_delete_section:
				showDeleteSectionDialog();
				break;
			default:
				break;
			}

		} else if (currItem instanceof ItemObject) {
			switch (itemId) {
			case R.id.contextmenu_listview_object_item_update:
				showUpdateDialog();
				break;
			case R.id.contextmenu_listview_object_item_delete:
				showDeleteItemDialog();
				break;
			default:
				break;
			}

		}
		adapter.notifyDataSetChanged();
		return super.onContextItemSelected(item);

	}

	private void fillDB() {

		itemDataSource.open();

		ItemSection itemSection1 = new ItemSection("section1");
		ItemSection itemSection2 = new ItemSection("section2");

		long itemId1 = itemDataSource.createItem(itemSection1);
		long itemId2 = itemDataSource.createItem(itemSection2);

		ItemObject itemObject1 = new ItemObject("Title1", "Status1", 0, itemId2);
		ItemObject itemObject2 = new ItemObject("Title2", "Status2", 0, itemId2);
		ItemObject itemObject3 = new ItemObject("Title3", "Status3", 0, itemId2);
		ItemObject itemObject4 = new ItemObject("Title4", "Status4", 0, itemId1);

		itemDataSource.createItem(itemObject1);
		itemDataSource.createItem(itemObject2);
		itemDataSource.createItem(itemObject3);
		itemDataSource.createItem(itemObject4);

		itemDataSource.close();
	}

	private void showCreateDialog() {
		dialogCreate = new Dialog(this);
		dialogCreate.setTitle("New item");
		dialogCreate.setContentView(R.layout.dialog_create_new_item);

		createNewObjectFieldTitle = (TextView) dialogCreate
				.findViewById(R.id.dialog_create_new_item_field_title);
		createNewObjectFieldStatus = (TextView) dialogCreate
				.findViewById(R.id.dialog_create_new_item_field_status);
		createNewObjectFieldPrice = (TextView) dialogCreate
				.findViewById(R.id.dialog_create_new_item_field_price);
		Spinner createNewObjectFieldSpinnerSections = (Spinner) dialogCreate
				.findViewById(R.id.dialog_create_new_item_spinner_section);
		Button createNewObjectButtonOk = (Button) dialogCreate
				.findViewById(R.id.dialog_create_new_item_button_ok);
		Button createNewObjectButtonCancel = (Button) dialogCreate
				.findViewById(R.id.dialog_create_new_item_button_cancel);

		itemDataSource.open();
		createNewObjectSectionsList = itemDataSource.getAllItemSectionsList();
		itemDataSource.close();

		List<String> spinnerObjects = new ArrayList<String>();
		for (int i = 0; i < createNewObjectSectionsList.size(); i++) {
			spinnerObjects.add(Long.toString(createNewObjectSectionsList.get(i)
					.getId()));
		}
		ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, spinnerObjects);
		createNewObjectFieldSpinnerSections.setAdapter(spinnerAdapter);

		createNewObjectFieldSpinnerSections
				.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> parent, View v,
							int position, long id) {
						createNewObjectSpinnerPosition = position;
					}

					@Override
					public void onNothingSelected(AdapterView<?> parent) {
						createNewObjectSpinnerPosition = 0;
					}
				});
		createNewObjectButtonOk.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String titleOfNewItem = createNewObjectFieldTitle.getText()
						.toString();
				String statusOfNewItem = createNewObjectFieldStatus.getText()
						.toString();
				int priceOfNewItem = Integer
						.parseInt((String) createNewObjectFieldPrice.getText()
								.toString());
				long sectionOfNewItem = createNewObjectSectionsList.get(
						createNewObjectSpinnerPosition).getId();
				ItemObject itemObjectNew = new ItemObject(titleOfNewItem,
						statusOfNewItem, priceOfNewItem, sectionOfNewItem);
				itemDataSource.open();
				itemDataSource.createItem(itemObjectNew);
				itemDataSource.close();
				ArrayList<Item> distributedData = takeDistributedDataFromDB();
				adapter.setItems(distributedData);
				dialogCreate.dismiss();
			}
		});
		createNewObjectButtonCancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialogCreate.dismiss();
			}
		});
		dialogCreate.show();
	}

	private void showDeleteSectionDialog() {
		// TODO Auto-generated method stub
		AlertDialog.Builder deleteDialog = new AlertDialog.Builder(this);
		deleteDialog.setMessage("Are you sure you want to delete this section?");
		deleteDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				ItemSection curSection = (ItemSection)adapter.getItem(currentListPosition);
				itemDataSource.open();
				itemDataSource.deleteSection(curSection);
				itemDataSource.close();

				itemDataSource.open();
				ArrayList<Item> distributedItems = takeDistributedDataFromDB();
				itemDataSource.close();
				
				adapter.setItems(distributedItems);
			}
			
		});
		deleteDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
		
		AlertDialog alert = deleteDialog.create();
		alert.show();
	}
	
	private void showDeleteItemDialog() {
		//TODO
		
		AlertDialog.Builder deleteDialog = new AlertDialog.Builder(this);
		deleteDialog.setMessage("Are you sure you want to delete this item?");
		deleteDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				ItemObject curItemObject = (ItemObject)adapter.getItem(currentListPosition);
				itemDataSource.open();
				itemDataSource.deleteItem(curItemObject);
				itemDataSource.close();
				adapter.deleteItem(currentListPosition);
				adapter.notifyDataSetChanged();
			}
		});
		deleteDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
		AlertDialog alert = deleteDialog.create();
		alert.show();
	}
	
	
	private void showCreateNewSectionDialog() {
		dialogCreateNewSection = new Dialog(this);
		dialogCreateNewSection.setTitle("Create section");
		dialogCreateNewSection.setContentView(R.layout.dialog_create_new_section);
		
		createNewItemObjectFieldName = (TextView)dialogCreateNewSection.findViewById(R.id.dialog_create_section_field);
		Button createNewItemSectionButtonOk = (Button)dialogCreateNewSection.findViewById(R.id.dialog_create_section_button_ok);
		Button createNewItemSectionButtonCancel = (Button)dialogCreateNewSection.findViewById(R.id.dialog_create_section_button_cancel);
		
		createNewItemSectionButtonOk.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String newSectionName = "default name"; 
				String createNewItemObjectFieldValue = createNewItemObjectFieldName.getText().toString();
				if(!createNewItemObjectFieldValue.equals("")) {
					newSectionName = createNewItemObjectFieldValue;
				}
				ItemSection newSection = new ItemSection(newSectionName);
				itemDataSource.open();
				itemDataSource.createItem(newSection);
				itemDataSource.close();
				
				
				ArrayList<Item> newDataForListView = takeDistributedDataFromDB();
				
				adapter.setItems(newDataForListView);
				dialogCreateNewSection.dismiss();
			}
		});
		
		createNewItemSectionButtonCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dialogCreateNewSection.dismiss();
			}
		});
		
		dialogCreateNewSection.show();
	}

	private void showSortRelatedItemObjectsDialog(int position) {
		dialogSortRelatedItemObjects = new Dialog(myListView.getContext());
		dialogSortRelatedItemObjects.setTitle("Sort related items by...");
		dialogSortRelatedItemObjects
				.setContentView(R.layout.dialog_sort_related_item_objects);

		sortRelatedItemsRadioButtonTitle = (RadioButton) dialogSortRelatedItemObjects
				.findViewById(R.id.dialog_sort_related_item_objects_radiobutton_title);
		sortRelatedItemsRadioButtonStatus = (RadioButton) dialogSortRelatedItemObjects
				.findViewById(R.id.dialog_sort_related_item_objects_radiobutton_status);
		sortRelatedItemsRadioButtonPrice = (RadioButton) dialogSortRelatedItemObjects
				.findViewById(R.id.dialog_sort_related_item_objects_radiobutton_price);

		Button sortRelatedItemsButtonOk = (Button) dialogSortRelatedItemObjects
				.findViewById(R.id.dialog_sort_related_item_objects_button_ok);
		Button sortRelatedItemsButtonCancel = (Button) dialogSortRelatedItemObjects
				.findViewById(R.id.dialog_sort_related_item_objects_button_cancel);

		sortRelatedItemsButtonOk.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Item currItem = adapter.getItem(currentListPosition);
				int sortBy = 0;
				
				if (sortRelatedItemsRadioButtonTitle.isChecked()) {
					sortBy = Tools.ItemObjectSort.TITLE;
				} else if (sortRelatedItemsRadioButtonStatus.isChecked()) {
					sortBy = Tools.ItemObjectSort.STATUS;
				} else if (sortRelatedItemsRadioButtonPrice.isChecked()) {
					sortBy = Tools.ItemObjectSort.PRICE;
				}
				
				for (int i = 0; i < sectionsList.size(); i++) {
					if (sectionsList.get(i).getId() == ((ItemSection) currItem)
							.getId()) {
						sectionsList.get(i).sortNativeItemObjectsBy(sortBy);
					}

				}
				
				ArrayList<Item> distributedData = distributeItems();
				adapter.setItems(distributedData);
				
				dialogSortRelatedItemObjects.dismiss();
			}
		});

		sortRelatedItemsButtonCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialogSortRelatedItemObjects.dismiss();
			}
		});

		//TODO RAdiogroup
		sortRelatedItemsRadioButtonTitle
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							sortRelatedItemsRadioButtonStatus.setChecked(false);
							sortRelatedItemsRadioButtonPrice.setChecked(false);
						}
					}
				});

		sortRelatedItemsRadioButtonStatus
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						sortRelatedItemsRadioButtonTitle.setChecked(false);
						sortRelatedItemsRadioButtonPrice.setChecked(false);
					}
				});

		sortRelatedItemsRadioButtonPrice
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						sortRelatedItemsRadioButtonTitle.setChecked(false);
						sortRelatedItemsRadioButtonStatus.setChecked(false);
					}
				});
		dialogSortRelatedItemObjects.show();
	}

	private void addItemToSection(ItemObject itemObject) {
		for (int i = 0; i < sectionsList.size(); i++) {
			ItemSection curItemSection = sectionsList.get(i);
			if (curItemSection.getId() == itemObject.getSectionId()) {
				curItemSection.addNativeItemObject(itemObject);
			}
		}
	}

	private void showUpdateDialog() {
		dialogUpdate = new Dialog(myListView.getContext());
		dialogUpdate.setTitle("Edit");
		dialogUpdate.setContentView(R.layout.edit_data_layout);
		fieldTitle = (TextView) dialogUpdate.findViewById(R.id.field_title);
		fieldPrice = (TextView) dialogUpdate.findViewById(R.id.field_price);
		fieldStatus = (TextView) dialogUpdate.findViewById(R.id.field_status);
		updateObjectButtonOk = (Button) dialogUpdate
				.findViewById(R.id.button_ok);
		updateObjectButtonCancel = (Button) dialogUpdate
				.findViewById(R.id.button_cancel);
		updateObjectButtonCancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialogUpdate.dismiss();
			}
		});
		
		updateObjectButtonOk.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Tools.print(Integer.toString(currentListPosition));
				ItemObject curentItem = (ItemObject) adapter
						.getItem(currentListPosition);
				String newTitle = fieldTitle.getText().toString();
				int newPrice = Integer
						.parseInt(fieldPrice.getText().toString());
				String newStatus = fieldStatus.getText().toString();
				curentItem.setTitle(newTitle);
				curentItem.setStatus(newStatus);
				curentItem.setPrice(newPrice);
				
				adapter.setItem(currentListPosition, curentItem);
				adapter.notifyDataSetChanged();
				
				itemDataSource.open();
				itemDataSource.updateItemObject(curentItem);
				itemDataSource.close();
				dialogUpdate.dismiss();
			}
		});
		fieldTitle.setText(((ItemObject) adapter.getItem(currentListPosition))
				.getTitle());
		fieldPrice.setText(Integer.toString((((ItemObject) adapter
				.getItem(currentListPosition)).getPrice())));
		fieldStatus.setText(((ItemObject) adapter.getItem(currentListPosition))
				.getStatus());
		dialogUpdate.show();
	}

	private void showSortSectionsDialog() {
		dialogSortSections = new Dialog(this); // TODO AlertDialog
		dialogSortSections.setTitle("Sort sections");
		dialogSortSections.setContentView(R.layout.dialog_sort_sections_layout);

		sortSectionsDialogRadioButtonDesc = (RadioButton) dialogSortSections
				.findViewById(R.id.dialog_sort_sections_radiobutton_sortdesc); // rb_dialogsort_desc
		sortSectionsDialogRadioButtonAsc = (RadioButton) dialogSortSections
				.findViewById(R.id.dialog_sort_sections_radiobutton_sortasc);
		Button sortSectionsDialogButtonOk = (Button) dialogSortSections
				.findViewById(R.id.dialog_sort_sections_button_ok);
		Button sortSectionsDialogButtonCancel = (Button) dialogSortSections
				.findViewById(R.id.dialog_sort_sections_button_cancel);

		sortSectionsDialogRadioButtonAsc
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							sortSectionsDialogRadioButtonDesc.setChecked(false);
						}
					}
				});

		sortSectionsDialogRadioButtonDesc
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							sortSectionsDialogRadioButtonAsc.setChecked(false);
						}
					}
				});

		sortSectionsDialogButtonOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (sortSectionsDialogRadioButtonAsc.isChecked()) {
					sortSections(Tools.SectionSort.ASC);
				} else if (sortSectionsDialogRadioButtonDesc.isChecked()) {
					sortSections(Tools.SectionSort.DESC);
				}
				ArrayList<Item> distributedData = distributeItems();
				adapter.setItems(distributedData);
				dialogSortSections.dismiss();
			}
		});

		sortSectionsDialogButtonCancel
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						dialogSortSections.dismiss();
					}
				});

		dialogSortSections.show();
	}

	private void sortSections(int order) {
		class SectionsComparator implements Comparator<ItemSection> {
			@Override
			public int compare(ItemSection first, ItemSection second) {
				int result = first.getTitle().compareTo(second.getTitle());
				return result;
			}
		}
		
		switch (order) {
		case Tools.SectionSort.ASC:
			Collections.sort(sectionsList, new SectionsComparator());
			Collections.reverse(sectionsList);
			;
			break;
		case Tools.SectionSort.DESC:
			Collections.sort(sectionsList, new SectionsComparator());
			break;
		default:
			Collections.sort(sectionsList, new SectionsComparator());
		}
	}

//	private ArrayList<Item> takeData() {
//		ArrayList<Item> items = new ArrayList<Item>();
//		ItemObject d1 = new ItemObject();
//		d1.setStatus("status1");
//		d1.setImageId(android.R.drawable.ic_delete);
//		ItemObject d2 = new ItemObject();
//		d1.setImageId(android.R.drawable.ic_input_add);
//		d2.setStatus("status2");
//		ItemObject d3 = new ItemObject();
//		d3.setImageId(android.R.drawable.ic_media_next);
//		d3.setStatus("status3");
//		ItemObject d4 = new ItemObject();
//		d3.setImageId(android.R.drawable.ic_media_next);
//		d3.setStatus("status");
//		ItemSection s1 = new ItemSection();
//		s1.setTitle("name1");
//		ItemSection s2 = new ItemSection();
//		s2.setTitle("name2");
//		items.add(s1);
//		items.add(d1);
//		items.add(s2);
//		items.add(d2);
//		items.add(d3);
//		items.add(d4);
//		return items;
//	}

	private ArrayList<Item> joinList(ArrayList<ItemSection> listSections,
			ArrayList<ItemObject> listObjects) {
		ArrayList<Item> result = new ArrayList<Item>();
		for (int i = 0; i < listSections.size(); i++) {
			long currSectionId = ((ItemSection) listSections.get(i)).getId();
			result.add(listSections.get(i));
			for (int j = 0; j < listObjects.size(); j++) {
				ItemObject currItemObject = ((ItemObject) listObjects.get(j));
				if (currItemObject.getSectionId() == currSectionId) {
					result.add(currItemObject);
				}
			}
		}
		return result;
	}

	private void prepareList() {
		// ArrayList<Item> items = takeDataFromDB();
		ArrayList<Item> items = takeDistributedDataFromDB();
		adapter = new MyDataAdapter(this, R.layout.list_item_layout, items);
		myListView.setAdapter(adapter);
		myListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				currentListPosition = position;
				Intent intent = new Intent(context,
						ItemDescriptionActivity.class);
				
				Item curItem = adapter
						.getItem(currentListPosition);
				if(curItem instanceof ItemObject) {
					ItemObject curItemObject = (ItemObject) curItem;
					String data = prepareDataForDescriptionView(curItemObject);
					intent.putExtra(getString(R.string.item_object_image_id),
							curItemObject.getImageId());
					intent.putExtra(getString(R.string.item_object_data), data);	
					startActivity(intent);
				} 
			}
		});

//		myListView.setOnItemLongClickListener(new OnItemLongClickListener() {
//
//			@Override
//			public boolean onItemLongClick(AdapterView<?> parent, View v,
//					int position, long id) {
//				Tools.print("On long click works");
//				currentListPosition = position;
//				return false;
//			}
//		});

	}

	private ArrayList<Item> takeDataFromDB() {
		itemDataSource.open();
		ArrayList<Item> items = new ArrayList<Item>();
		ArrayList<ItemObject> listObjects = itemDataSource
				.getAllItemObjectsList();
		ArrayList<ItemSection> listSections = itemDataSource
				.getAllItemSectionsList();
		itemDataSource.close();
		items = joinList(listSections, listObjects);
		return items;
	}

	/**
	 * @return distributed view of current existing item sections
	 */
	private ArrayList<Item> distributeItems() {
		ArrayList<Item> items = new ArrayList<Item>();
		for (int i = 0; i < sectionsList.size(); i++) {
			ArrayList<ItemObject> itemObjectsOfCurSection = sectionsList.get(i)
					.getNativeItemObjects();
			items.add(sectionsList.get(i));
			items.addAll(itemObjectsOfCurSection);
		}
		return items;
	}

	/**
	 * @return list got from db, ready to show it in listview
	 */
	private ArrayList<Item> takeDistributedDataFromDB() {
		distributeItemObjectsFromDB();
		ArrayList<Item> items = distributeItems();
		return items;
	}

	/**
	 * takes all data from db and distributes it to sectionsList and
	 * itemObjectsList
	 */
	private void distributeItemObjectsFromDB() {
		itemDataSource.open();
		sectionsList = itemDataSource.getAllItemSectionsList();
		itemObjectsList = itemDataSource.getAllItemObjectsList();
		itemDataSource.close();
		for (int i = 0; i < itemObjectsList.size(); i++) {
			ItemObject curItemObject = itemObjectsList.get(i);
			for (int j = 0; j < sectionsList.size(); j++) {
				if (curItemObject.getSectionId() == sectionsList.get(j).getId()) {
					sectionsList.get(j).addNativeItemObject(curItemObject);
				}
			}
		}
	}

	private String prepareDataForDescriptionView(ItemObject object) {
		StringBuilder builder = new StringBuilder();
		builder.append("Title: ");
		builder.append(object.getTitle());
		builder.append("\n");
		builder.append("Price: ");
		builder.append(object.getPrice());
		builder.append("\n");
		builder.append("Status: ");
		builder.append(object.getStatus());
		String data = builder.toString();
		return data;
	}

	private void mapUI() {
		context = this.getApplicationContext();
		headerText = (TextView) findViewById(R.id.text_header);
		headerText.setText(getString(R.string.app_name));
		myListView = (ListView) findViewById(R.id.my_list_view);
	}

	private void prepareListeners() {

	}
}
